//
//  JXLStatusOriginalView.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/24.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit

//微博的原创微博视图
class JXLStatusOriginalView: UIView {

    //纪录orginalView的底部约束
    var originViewBottomConstraint: Constraint?
    
    //拿到从cell传过来数据模型，给子控件复制
    var statusViewModel: JXLStatusViewModel? {
        didSet {
            //用户名字
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //用户头像
           if let userImage = statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImage(with: URL(string:userImage), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //时间label
            timeLabel.text = statusViewModel?.timeContent
            //内容
            contentLabel.text = statusViewModel?.status?.text
            //会员等级图片
            mbrankImageView.image = statusViewModel?.mbrankImage
            //认证类型等级
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            //来源内容
            sourceLabel.text = statusViewModel?.sourceContent
            
            //卸载三次约束
            originViewBottomConstraint?.uninstall()
            
            //判断原创微博的配图个数是否大于0
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //显示配图
                imageView.isHidden = false
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    originViewBottomConstraint = make.bottom.equalTo(imageView).offset(JXLHomeTableViewCellMargin).constraint
                    
                })
                //  绑定数据源
                imageView.picUrls = picUrls
            } else {
                //隐藏配图
                imageView.isHidden = true
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    originViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(JXLHomeTableViewCellMargin).constraint
                    
                })

            }
        }
    }
    
    //  MARK: --    懒加载控件
    //  用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //  认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
//    private lazy var screenNameLabel: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
//        //label.text = "小逗比神吐糟"
//        return label
//    }()
    private lazy var screenNameLabel: UILabel = UILabel(fontSize: 14, textColor: .darkGray)
   
    //  会员等级图片
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //  时间
//    private lazy var timeLabel: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
//        //label.text = "刚刚"
//        return label
//    }()
    private lazy var timeLabel: UILabel = UILabel(fontSize: 12, textColor: .darkGray)
   
    //  来源
//    private lazy var sourceLabel: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
//        label.text = "来自星星的你"
//        return label
//    }()
    private lazy var sourceLabel: UILabel = UILabel(fontSize: 12, textColor: .darkGray)
    
    //  微博内容
    private lazy var contentLabel: UILabel = {
        //        let label = UILabel()
        //        label.font = UIFont.systemFont(ofSize: 14)
        //        label.textColor = UIColor.darkGray
        // label.text = "听说下雨天音乐跟辣条更配哟~听说下雨天音乐跟辣条更配哟~听说下雨天音乐跟辣条更配哟~"
        //  多行显示
        let label = UILabel(fontSize: 14, textColor: .darkGray)
        label.numberOfLines = 0
        return label
        
    }()
    
    //原创微博配图视图
    private lazy var imageView: JXLStatusPictureView = {
       
        let view = JXLStatusPictureView()
        //  原创微博配图颜色等于原创微博视图的颜色
        view.backgroundColor = self.backgroundColor
        
        return view
    }()
    
    
    // MARK: 重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        
        backgroundColor = UIColor.white
        
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(imageView)
        
        //  设置约束
        userImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(JXLHomeTableViewCellMargin)
            make.leading.equalTo(self).offset(JXLHomeTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(JXLHomeTableViewCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(JXLHomeTableViewCellMargin)
        }

        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(JXLHomeTableViewCellMargin)
            make.bottom.equalTo(userImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(JXLHomeTableViewCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView.snp_bottom).offset(JXLHomeTableViewCellMargin)
            make.leading.equalTo(userImageView)
            make.width.equalTo(ScreenWidth - 2 * JXLHomeTableViewCellMargin)
        }
        
        imageView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(JXLHomeTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        //关键约束
       self.snp_makeConstraints { (make) in
           originViewBottomConstraint = make.bottom.equalTo(imageView).offset(JXLHomeTableViewCellMargin).constraint
        }

    }


}
